<!--
 * @Author: yangmy
 * @Date: 2022-03-10 18:06:33
 * @FileDescription:当前涨跌
-->
<template>
  <com-box :title="title">
    <template #titleRight>
      <slot name="titleRight" />
    </template>
    <template v-slot>
      <loading v-show="loading" />
      <div class="time" v-if="data.time">期号：{{ data.time }}</div>
      <div class="goods">
        <div>
          <div
            class="goods-list"
            @click="handGoodsClick(item.goodsName, item.goodsCode)"
            v-for="(item, index) in data.szData"
            :key="index"
          >
            <div class="goods-left">
              <div class="goods-box">
                <img
                  :src="
                    item.img && '' + item.img.slice(item.img.indexOf('/images'))
                  "
                  class="goods-img"
                />
                <div class="name-unit">
                  <text class="name">{{
                    item.goodsName.indexOf('芹菜') > -1
                      ? '芹菜'
                      : item.goodsName
                  }}</text>
                  <text class="unit"> {{ item.unit }}</text>
                </div>
              </div>
            </div>
            <div class="goods-right">
              <div class="goods-box">
                <div class="price">
                  <span>￥</span>
                  {{ item.avgPrice }}
                </div>

                <div class="proportion">
                  <div class="red">
                    {{ item.avgPriceD2D }}
                  </div>

                  <div class="point red">
                    <img src="~@/assets/images/upper.png" alt="" />
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div>
          <div
            class="goods-list"
            @click="handGoodsClick(item.goodsName, item.goodsCode, item.unit)"
            v-for="(item, index) in data.xdData"
            :key="index"
          >
            <div class="goods-left">
              <div class="goods-box">
                <img
                  :src="
                    item.img && '' + item.img.slice(item.img.indexOf('/images'))
                  "
                  class="goods-img"
                />
                <div class="name-unit">
                  <text class="name">{{ item.goodsName }}</text>
                  <text class="unit"> {{ item.unit }}</text>
                </div>
              </div>
            </div>
            <div class="goods-right">
              <div class="goods-box">
                <div class="price">
                  <span>￥</span>
                  {{ item.avgPrice }}
                </div>
                <div class="proportion">
                  <div class="green">
                    {{ item.avgPriceD2D }}
                  </div>
                  <div class="point green">
                    <img src="~@/assets/images/lower.png" alt="" />
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </template>
  </com-box>
</template>
<script lang="ts">
import { defineComponent, reactive, toRefs, watch, computed } from 'vue'
import { useStore } from 'vuex'
import comBox from '@/components/comBox/comBox.vue'
import loading from '@/charts/loading.vue'
import httpRequest from '@/service'
interface IState {
  title: string
  time: string
  loading: boolean
  data: any
  getData: (adcode: string, goodsId: string) => void
}
export default defineComponent({
  name: 'LeftTop',
  components: {
    comBox,
    loading
  },
  props: {
    adcode: {
      type: String,
      default: ''
    },
    goodsIds: {
      type: String,
      default: ''
    }
  },
  setup(props, { emit }) {
    const state: IState = reactive({
      title: '',
      time: '',
      loading: false,
      data: {},
      getData: (adcode: string, goodsId: string) => {
        state.loading = true
        httpRequest
          .get({ url: 'M2P1yjjczxqk', params: { adcode, goodsId } })
          .then((res) => {
            if (res.code == 200) {
              state.title = res.title
              state.time = res.time
              const resData = JSON.parse(JSON.stringify(res))
              while (resData.szData.length < 3) {
                resData.szData.push({
                  avgPrice: '',
                  avgPriceD2D: '',
                  goodsCode: '',
                  goodsName: '',
                  img: '',
                  unit: ''
                })
              }
              while (resData.xdData.length < 3) {
                resData.xdData.push({
                  avgPrice: '',
                  avgPriceD2D: '',
                  goodsCode: '',
                  goodsName: '',
                  img: '',
                  unit: ''
                })
              }
              state.data = resData
            }
            state.loading = false
          })
          .catch(() => {
            console.log('请求失败')
            state.loading = false
          })
      }
    })
    const handGoodsClick: any = (name: string, id: string, unit: string) => {
      emit('change', id, name, unit)
    }
    watch(
      () => [props.adcode, props.goodsIds],
      ([adVal, goodsVal]) => {
        if (!adVal) {
          return false
        }
        state.getData(adVal, goodsVal)
      }
    )
    state.getData(props.adcode, props.goodsIds)
    const { title, time, data, loading } = toRefs(state)
    const refreshData = () => {
      state.getData(props.adcode, props.goodsIds)
    }
    return {
      title,
      time,
      data,
      loading,
      handGoodsClick,
      refreshData
    }
  }
})
</script>
<style lang="less" scoped>
.time {
  color: rgb(70, 237, 232);
  text-align: right;
  font-weight: 600;
  padding-right: 18px;
  font-family: 'Microsoft YaHei';
  font-size: 14px;
}
.goods {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  margin-bottom: 10px;
}
.goods-list {
  cursor: pointer;
  background-size: 210px 71px;
  margin: 3.5px 3px;
  background-color: rgba(0, 104, 183, 0.2);
  width: 210px;
  height: 71px;
  z-index: 365;
  display: flex;
  .goods-left {
    width: 50%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    .goods-box {
      width: 100%;
      height: 100%;
      position: relative;
      .goods-img {
        margin: 0 auto;
        display: block;
        height: 56px;
      }
      .name-unit {
        position: absolute;
        bottom: 5px;
        left: 7px;
        text-align: center;
        font-size: 11px;
        font-family: 'FZZZHONGJW';
        font-weight: 600;
        color: #fff;
        .name {
          padding: 0 2px 0 0;
        }
        .unit {
          padding: 0 0 0 2px;
        }
      }
    }
  }
  .goods-right {
    width: 50%;
    height: 100%;
    .goods-box {
      height: 100%;
      .price {
        font-size: 14px;
        font-family: 'AliHYAiHei';
        color: rgb(193, 253, 255);
        line-height: 1.743;
        padding-top: 10px;
        span {
          font-family: 'Microsoft YaHei';
          color: rgb(193, 211, 255);
          line-height: 1.071;
        }
      }
      .proportion {
        font-size: 13px;
        font-family: 'Microsoft YaHei';
        line-height: 2.614;
        display: flex;
        align-items: center;
        position: relative;
        font-weight: 550;
        .green {
          font-size: 13px;
          color: rgb(66, 255, 0);
          bottom: 6px;
        }
        .red {
          font-size: 13px;
          color: rgb(255, 38, 38);
          bottom: 7px;
        }
        .point {
          position: absolute;
          right: 20px;
          border-radius: 50%;
          width: 25px;
          height: 25px;
          text-align: center;
        }
      }
    }
  }
}
.left-lower {
  margin-top: 30px;
}
// 单位
.Price-unite-time {
  font-size: 10px;
  font-family: 'Microsoft YaHei';
  color: rgba(162, 199, 255, 0.902);
  font-weight: bold;
  line-height: 1.714;
  display: flex;
  justify-content: space-between;
  align-content: center;
  .unite {
    margin-left: 18px;
  }
  .time {
    align-content: center;
  }
}
</style>
